<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatat_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-s-custom" type="text"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //绑定登录表单数据的对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resLoginForm() {
      //重置表单框
      this.$refs.loginFormRef.resetFields();
    },
    // 登录验证
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        //发送登录请求
        this.$http.post("login", this.loginForm).then((res) => {
          if (res.data.meta.status != 200) {
            // console.log("账号或密码错误");
            this.$message.error("账号或密码错误");
            return;
          }
          this.$message({
            message: "登录成功",
            type: "success",
          });
          window.sessionStorage.setItem("token", res.data.data.token);
          this.$router.push("/home");
        });
      });
    },
  },
};
</script>

<style>
.login_container {
  height: 100%;
  background: url("../assets/images/bg.jpg") no-repeat top center;
  background-size: 100% 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatat_box {
  width: 130px;
  height: 130px;
  border: 10px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatat_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>